﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiJu
{
    public partial class SystemLog : UserControl
    {
        public SystemLog()
        {
            InitializeComponent();


        }
        private void SystemLog_Load(object sender, EventArgs e)
        {
            //加载combobox数据
            //string 

            //查询出所有菜单
            string sql = "select id,MenuName from systemmenu where MenuState=1 and MenuType='菜单'";

            var menuList = UserOpMysql.ExecuteDataTable(sql); ;
            if (menuList.Rows.Count > 0)
            {
                this.CbxoModel.DataSource = menuList;
                this.CbxoModel.DisplayMember = "MenuName";
                this.CbxoModel.ValueMember = "id";
            }

            this.CbxoModel.Text = "";



            DateEndTime.Value = DateEndTime.Value.ToLongDateString() == DateTime.Today.ToLongDateString() ? DateTime.Today.AddDays(1) : DateEndTime.Value; //结束日期


            DgvSysInfo.DataSource = null;
            //查询方案
            LoadSystemLogInfo();
        }


        // 近15天的数据
        private void Last15daybutton_Click(object sender, EventArgs e)
        {
            //设置15天前的日期
            DateBeginTime.Value = DateTime.Today.AddDays(-15);

            //查询方案
            LoadSystemLogInfo();

        }

        private void Last30daybutton_Click(object sender, EventArgs e)
        {

            //设置30天前的日期
            DateBeginTime.Value = DateTime.Today.AddDays(-30);

            //查询方案
            LoadSystemLogInfo();
        }

        private void Last90daybutton_Click(object sender, EventArgs e)
        {

            //设置90天前的日期
            DateBeginTime.Value = DateTime.Today.AddDays(-90);

            //查询方案
            LoadSystemLogInfo();
        }


        private void Searchbutton_Click(object sender, EventArgs e)
        {

            DateEndTime.Value = DateEndTime.Value.ToLongDateString() == DateTime.Today.ToLongDateString() ? DateTime.Today.AddDays(1) : DateEndTime.Value; //结束日期

            //查询方案
            LoadSystemLogInfo();

        }

        private void Resetbutton_Click(object sender, EventArgs e)
        {
            DateBeginTime.Value = DateTime.Now;//开始日期
            DateEndTime.Value = DateTime.Now.AddDays(1); //结束日期
            TbxOperateNo.Text = "";//操作人账号
            TbxOperateName.Text = "";//操作人名称
            CbxoModel.Text = "";  //操作模块

            //查询方案
            LoadSystemLogInfo();
        }

       

        /// <summary>
        /// 查询日志方法 
        /// </summary>
        /// <param name="begindate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="operateNo">操作人账号</param>
        /// <param name="operateName">操作人名称</param>
        /// <param name="model">操作模块</param>
        public void LoadSystemLogInfo()
        {
            //DateTime begindate, DateTime enddate, string operateNo, string operateName, string model



            DgvSysInfo.DataSource = null;

            string begindate = ((DateTime)DateBeginTime.Value).Date.ToString(); //开始日期
            string enddate = ((DateTime)DateEndTime.Value).Date.ToString(); //结束日期
            string operateNo = TbxOperateNo.Text;//操作人账号
            string operateName = TbxOperateName.Text;//操作人名称
            string model = CbxoModel.Text == "主目录" ? "" : CbxoModel.Text;  //操作模块


            //存储过程名称
            string procedureName = "QuerySystemLog";

            //定义参数 MySql 的参数必须以 ? 声明，而不是如Sqlserver 使用@
            List<MySqlParameter> mySqlParameters = new List<MySqlParameter>()
            {
                new  MySqlParameter("?in_StartDate",MySqlDbType.VarChar){ Value=begindate },
                new  MySqlParameter("?in_EndDate",MySqlDbType.VarChar){   Value=enddate   },
                new  MySqlParameter("?in_OperateNo",MySqlDbType.VarChar){   Value=operateNo   },
                new  MySqlParameter("?in_OperateName",MySqlDbType.VarChar){   Value=operateName   },
                new  MySqlParameter("?in_Model",MySqlDbType.VarChar){   Value=model   }
            };

            //调用封装的执行存储过程返回DataTable方法
            var sysList = UserOpMysql.ExecuteStoredProcedureToDataTable(procedureName, mySqlParameters);
            if (sysList.Rows.Count > 0)
            {
                DgvSysInfo.DataSource = sysList;
            }
            else
            {
                MessageBox.Show("没有查询到相关的数据，请检查过滤条件！", "过滤条件有误！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                DgvSysInfo.DataSource = null;
            }
        }

        private void TbxOperateNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || Char.IsLetter(e.KeyChar))//输入1-8位数字或字母
            {
                if (TbxOperateNo.Text.Length <= 7)
                {
                    e.Handled = false;
                }
                else if (TbxOperateNo.SelectionLength > 1)
                {
                    e.Handled = false;//选择多个
                }
                else
                {
                    e.Handled = true;
                }
            }

            //e.KeyChar == '.' ||
            else if (e.KeyChar == (char)Keys.Return) //Tab或回车键
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");
            }
            else if (e.KeyChar == '\b' || (int)e.KeyChar == 7)
                e.Handled = false;//删除
            else
                e.Handled = true;
        }

        private void TbxOperateName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || Char.IsLetter(e.KeyChar))//输入1-8位数字或字母
            {
                if (TbxOperateName.Text.Length <= 7)
                {
                    e.Handled = false;
                }
                else if (TbxOperateName.SelectionLength > 1)
                {
                    e.Handled = false;//选择多个
                }
                else
                {
                    e.Handled = true;
                }
            }

            //e.KeyChar == '.' ||
            else if (e.KeyChar == (char)Keys.Return) //Tab或回车键
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");
            }
            else if (e.KeyChar == '\b' || (int)e.KeyChar == 7)
                e.Handled = false;//删除
            else
                e.Handled = true;
        }

       
    }
}
